package com.yc.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.blog.entity.Article;
import com.yc.blog.mapper.ArticleMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 廖老师
 * @since 2023-01-04
 */
@RestController
@RequestMapping("/blog/article")
public class ArticleController {

    @Resource
    ArticleMapper articleMapper;

    @GetMapping("queryNew")
    public Object queryNew(
            @RequestParam(required = false) String title,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size){
        IPage<Article> pg = new Page<>(page, size);
        // 添加排序
        pg.orders().add(OrderItem.desc("createTime"));
        LambdaQueryWrapper<Article> lqw = new LambdaQueryWrapper<>();
        if (title!= null){
            lqw.like(Article::getTitle, "%"+title+"%");
        }
        IPage<Article> ret = articleMapper.selectPage(pg, lqw);
        // 如果是表格的分页查询，返回分页对象
        // return ret;
        // 如果是滚动加载，返回list
        return ret.getRecords();
    }

    @GetMapping("get")
    Article get(int id){
        return articleMapper.selectById(id);
    }
}

